﻿using B.S.BaseData.Domain.Entities.RBAC;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.ErrorCode.ErrorCodes;
using B.S.DataBase.EFCoreServiers.Dtos.Permissions;
using B.S.DataBase.EFCoreServiers.IServices.Permissions;
using Consul;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.ReadAPI.Controllers
{
    /// <summary>
    /// 权限管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "Perms")]
    public class PermissionController : ControllerBase
    {
        private readonly IPermissionServices permission;
        private readonly ILogger<PermissionController> _logger;

        public PermissionController(IPermissionServices permission, ILogger<PermissionController> logger)
        {
            this.permission = permission;
            _logger = logger;
        }
        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<IList<PermissionTree>>> GetPermissiontAsync(int roleId)
        {
            try
            {
                var res = await permission.GetPermissiontAsync(roleId);
                if (res != null)
                {
                    _logger.LogInformation("获取角色权限: ", roleId);
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult<IList<PermissionTree>>.Fail("获取角色权限出错：" + ex.Message, StatusCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetAllPermissiontDto>>> GetAllPermissiontAsync()
        {
            try
            {
                var res = await permission.GetAllPermissiontAsync();
                if (res != null)
                {
                    _logger.LogInformation("获取全部权限: ");
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult<IList<GetAllPermissiontDto>>.Fail(
                    "获取权限失败：" + ex.Message,
                    StatusCodeEnum.Exception
                );
            }
        }
        /// <summary>
        /// 获取菜单权限的树形
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<IList<GetMenuPermissionTree>>> GetMenuPermission(int ParentId = 0)
        {
            try
            {
                var res = await permission.GetMenuPermission(ParentId);
                if (res != null)
                {
                    _logger.LogInformation("获取全部权限: ");
                }
                return res;
            }
            catch (Exception ex)
            {
                return ApiResult<IList<GetMenuPermissionTree>>.Fail("获取菜单权限失败：" + ex.Message,
                    StatusCodeEnum.Exception
                );
            }

        }
    }
}
